package 优先算法.二分查找.二分查找704;

/**
 * https://leetcode.cn/problems/binary-search/description/
 */

public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {-1,0,3,5,9,12};
        int rel = so.search(arr, 9);
        System.out.println(rel);
    }
}
class Solution {
    public int search(int[] nums, int target) {
        int left = 0,right = nums.length-1,rel =-1;
        while(left <= right){
//            int mid =(right + left)>>1;//第一种
            int mid =left+(right - left)>>1;//第二种
            if(nums[mid] > target){
                right = mid -1;
            }else if (nums[mid] < target){
                left = mid +1;
            }else{
                rel = nums[mid];
                return rel;
            }
        }
        return rel;
    }
}